package com.dharma.mydiet.view;

import java.util.ArrayList;
import java.util.Date;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.dharma.mydiet.MainApplication;
import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOFood;
import com.dharma.mydiet.storage.DAORegister;
import com.dharma.mydiet.storage.DAORegisterFood;
import com.dharma.mydiet.storage.DAORegisterPersonal;
import com.dharma.mydiet.storage.TransactionalFood;
import com.dharma.mydiet.storage.TransactionalRegister;
import com.dharma.mydiet.storage.TransactionalRegisterFood;
import com.dharma.mydiet.storage.TransactionalRegisterPersonal;
import com.dharma.mydiet.storage.TransactionalUser;
import com.dharma.mydiet.utility.Utils;
import com.dharma.mydiet.utility.Utils.TextFieldType;
import com.google.analytics.tracking.android.EasyTracker;

public class NewRegisterActivity extends Activity {

    private Spinner registerType;
    private Spinner foodType;
    private TextView quantityFood;
    private TextView quantityPersonal;

    private Button saveButton;
    private DatePicker datePersonal;
    private TimePicker timePersonal;
    private DatePicker dateAlimento;
    private TimePicker timeAlimento;
    private ArrayList<TransactionalFood> foods;
    private ArrayList<TransactionalRegister> registers;
    private ArrayAdapter<CharSequence> adapterTypeRegister;
    private ArrayAdapter<CharSequence> adapterTypeFood;
    private LinearLayout foodRegister;
    private LinearLayout personalRegister;
    private Button newRegister;
    private Button saveRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.activity_new_consume);
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Nuevo Registro");

	foodRegister = (LinearLayout) findViewById(R.id.registroAlimento);
	personalRegister = (LinearLayout) findViewById(R.id.registroPersonal);
	newRegister = (Button) findViewById(R.id.NewTypeRegister);
	quantityFood = (TextView) findViewById(R.id.quantityFood);
	timeAlimento = (TimePicker) findViewById(R.id.timeAlimento);
	timePersonal = (TimePicker) findViewById(R.id.timePersonal);
	quantityPersonal = (TextView) findViewById(R.id.quantityPersonal);
	saveButton = (Button) findViewById(R.id.SaveButton);
	dateAlimento = (DatePicker) findViewById(R.id.dateAlimento);
	dateAlimento.setCalendarViewShown(false);
	datePersonal = (DatePicker) findViewById(R.id.datePersonal);
	datePersonal.setCalendarViewShown(false);
	saveRegister = (Button) findViewById(R.id.SaveButton);
	registerType = (Spinner) findViewById(R.id.TypeRegister);
	adapterTypeRegister = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterTypeRegister.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	registerType.setAdapter(adapterTypeRegister);

	foodType = (Spinner) findViewById(R.id.FoodType);
	adapterTypeFood = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterTypeFood.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	foodType.setAdapter(adapterTypeFood);

	newRegister.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(getApplicationContext(), NewRegisterTypeActivity.class);
		startActivity(intent);
	    }
	});
	registerType.setOnItemSelectedListener(new OnItemSelectedListener() {

	    @Override
	    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 == 0) {
		    foodRegister.setVisibility(View.VISIBLE);
		    personalRegister.setVisibility(View.GONE);
		} else {
		    foodRegister.setVisibility(View.GONE);
		    personalRegister.setVisibility(View.VISIBLE);
		}
	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	    }
	});

	saveButton.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (isValid()) {
		    int register = registerType.getSelectedItemPosition();
		    TransactionalFood food = (TransactionalFood) foods.get(foodType.getSelectedItemPosition());
		    TransactionalUser user = ((MainApplication) getApplication()).getCurrentUser();
		    if (register == 0) {
			Date date = Utils.getDateFromDatePicket(dateAlimento, timeAlimento);
			TransactionalRegisterFood foodRegister = new TransactionalRegisterFood(0, food.getIdFood(),
				user.getIdUser(), date.toString(), Integer.parseInt(quantityFood.getText().toString()));
			DAORegisterFood.getInstance().insertFood(foodRegister);
			finish();
		    } else {
			Date date = Utils.getDateFromDatePicket(datePersonal, timePersonal);
			TransactionalRegister registerSelected = (TransactionalRegister) registers.get(registerType
				.getSelectedItemPosition() - 1);
			TransactionalRegisterPersonal personalRegister = new TransactionalRegisterPersonal(0,
				registerSelected.getIdRegister(), user.getIdUser(), date.toString(), Integer
					.parseInt(quantityPersonal.getText().toString()));
			DAORegisterPersonal.getInstance().insertFood(personalRegister);
			finish();
		    }

		}
	    }

	});

    }

    private boolean isValid() {
	boolean isValid = true;
	int register = registerType.getSelectedItemPosition();
	if (register == 0) {
	    if (foodType.getSelectedItemPosition() == -1) {
		isValid = false;
		Toast.makeText(this, "Debe seleccionar algun alimento", Toast.LENGTH_LONG);
	    }

	    if (!Utils.isValidTextField(TextFieldType.NUMBER_FLOAT, quantityFood.getText().toString())) {
		quantityFood.setError("Entrada Invalida.");
		isValid = false;
	    }
	} else {
	    if (!Utils.isValidTextField(TextFieldType.NUMBER_FLOAT, quantityPersonal.getText().toString())) {
		quantityFood.setError("Entrada Invalida.");
		isValid = false;
	    }
	}
	return isValid;

    }

    @Override
    protected void onResume() {
	updateSpinners();
	super.onResume();

    }

    private void updateSpinners() {
	adapterTypeRegister.clear();
	registers = DAORegister.getInstance().getRegisters();
	if (registers != null) {
	    adapterTypeRegister.add("Alimento");
	    for (int i = 0; i < registers.size(); i++) {
		adapterTypeRegister.add(((TransactionalRegister) registers.get(i)).getName());
	    }

	}
	adapterTypeFood.clear();
	foods = DAOFood.getInstance().getFoods();
	for (int i = 0; i < foods.size(); i++) {
	    adapterTypeFood.add(((TransactionalFood) foods.get(i)).getName());
	}
    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
